package com.liliudong.makabaka.util;

import com.liliudong.makabaka.sys.entity.SysMenu;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 树形工具
 *
 * @author liudo
 * @version 1.0
 * @project makabaka-backend
 * @date 2023/9/24 11:32:22
 */
public class TreeUtil {

    /**
     * 获取子菜单
     *
     * @param parentMenuList 父菜单列表
     * @param sysMenuList
     * @return {@link List}<{@link SysMenu}>
     */
    public static List<SysMenu> getChildrenMenus(List<SysMenu> parentMenuList, List<SysMenu> sysMenuList) {
        parentMenuList.forEach(parent -> {
            List<SysMenu> children = sysMenuList.stream()
                    .filter(sysMenu -> Objects.equals(parent.getId(), sysMenu.getParentId()))
                    .collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(children)) {
                List<SysMenu> childrenMenus = getChildrenMenus(children, sysMenuList);
                parent.setChildren(childrenMenus);
            }
        });
        return parentMenuList;
    }
}
